home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9268 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.3 KB  |  37 lines

  1. Newsgroups: comp.lang.c
  2. Path: cwi.nl!dik
  3. From: dik@cwi.nl (Dik T. Winter)
  4. Subject: Re: HELP: Floating point precision
  5. Message-ID: <Dnz5Lw.KJ3@cwi.nl>
  6. Sender: news@cwi.nl (The Daily Dross)
  7. Nntp-Posting-Host: chrysant.cwi.nl
  8. Organization: CWI, Amsterdam
  9. References: <3140831B.1597@interport.com>
  10. Date: Sat, 9 Mar 1996 00:34:44 GMT
  11.  
  12. In article <3140831B.1597@interport.com> jeremy <jeremy@interport.com> writes:
  13.  > The mechanism for casting 4-byte float to 8-byte double is 
  14.  > causing me some problems.
  15.  
  16. Your problem is not with the cast but with understanding floating point
  17. arithmetic.
  18.  > 
  19.  >     float f;
  20.  >     double d;
  21.  >     f = 1.332;
  22.  >     d = (double) f;
  23.  > results in:
  24.  > f = 1.33200
  25.  > d = 1.3320000171661
  26.  
  27. A float is in general not able to represent all *decimal* numbers
  28. exactly.  A float is in binary.  For instance 1.332 is one of those
  29. numbers that have no exact representation in binary.  The closest
  30. binary number with 24 bits of precision (as your float probably has)
  31. is 1.3320000171661376953125 (and this one is exact).  So the cast
  32. correctly converts the float f to a double.  Try to print f with
  33. more digits of precision and see what happens.
  34. -- 
  35. dik t. winter, cwi, kruislaan 413, 1098 sj  amsterdam, nederland, +31205924098
  36. home: bovenover 215, 1025 jn  amsterdam, nederland; http://www.cwi.nl/~dik/
  37.